import re

from plugins import CheckPlugin
from helper import CallProcess, raw2lines
from config import logger

cmd = '/usr/bin/uptime'
proc = CallProcess(cmd)

class CheckLoad(CheckPlugin):
    topic = 'load'

    def check(self):
        logger.debug('%s check cmd: %s' % (self.topic, cmd))
        uptime_pat = re.compile(r'(\d+:\d+.M)\s+up\s+(\d+.*),\s\d+\susers?')
        users_pat = re.compile(r'(\d+)\susers?,')
        load_pat = re.compile(r'load\saverages:\s+(\d+\.\d+),\s+(\d+\.\d+),\s+(\d+\.\d+)')
        proc()
        line = raw2lines(proc.stdout)[0]
        start_time, up_time = uptime_pat.search(line).groups()
        users = users_pat.search(line).groups()[0]
        load = load_pat.search(line).groups()
        self.results = [{'start_time': start_time,
                         'up_time': up_time,
                         'users': users,
                         'load': load}]

